package ModelLayer;		//makes this class a part of the ModelLayer package
import java.io.Serializable;
import java.util.ArrayList;

/**
 * The Order class is a superclass to SaleOrder and RentOrder
 * @author (ucn-1semprojekt-dm78-gruppe1) 
 */
@SuppressWarnings("serial")
public abstract class Order implements Serializable
{
    private int ordernr;
    private String date;
    private double totalPrice;
    private Employee employee;
    private ArrayList<PartOrder> partOrders;
    private Customer customer;
    private int deliveryDiscount;
    private static int idGenerator = 0;

    /**
     * Constructor for objects of class Order
     */
    public Order(int ordernr, String date, double totalPrice, Employee employee, int deliveryDiscount, Customer customer)
    {
    	idGenerator++;
    	this.ordernr = idGenerator;
        this.date = date;
        this.totalPrice = totalPrice;
        this.employee = employee;
        partOrders = new ArrayList<PartOrder>();
        this.customer = customer;
        this.deliveryDiscount = deliveryDiscount;
    }
    public Order(ArrayList<PartOrder> partOrders)
    {
    	partOrders = new ArrayList<PartOrder>();
    }

    /**
     * Get the ordernr of an Order
     * @return ordernr
     */
    public int getOrdernr()
    {
        return ordernr;
    }

    /**
     * Get the date of an Order
     * @return date
     */
    public String getDate()
    {
        return date;
    }

    /**
     * Get the total price of an Order
     * @return totalPrice
     */
    public double getTotalPrice()
    {
        return totalPrice;
    }

    /**
     * Get the employee of an Order
     * @return employee
     */
    public Employee getEmployee()
    {
        return employee;
    }

    /**
     * Set the ordernr of an Order
     * @param newOrdernr
     */
    public void setOrdernr(int newOrdernr)
    {
    	this.ordernr = newOrdernr;
    }

    /**
     * Set the date of an Order
     * @param newDate
     */
    public void setDate(String newDate)
    {
    	this.date = newDate;
    }

    /**
     * Set the total price of an Order
     * @param newTotalPrice
     */
    public void setTotalPrice(double newTotalPrice)
    {
    	this.totalPrice = newTotalPrice;
    }

    /**
     * set the employee of an Order
     * @param newEmployee
     */
    public void setEmployee(Employee newEmployee)
    {
    	this.employee = newEmployee;
    }

    /**
     * Add a PartOrder to Order
     * @param partOrder
     */
    public void addPartOrder(PartOrder partOrder)
    {
    	this.partOrders.add(partOrder);
    }

    public PartOrder getPartOrder(int part)
    {
    	if (partOrders.size() <= part)
    		return partOrders.get(part);
    	else
    		return null;
    }
    
    /**
     * Add a Customer to Order
     * @param 
     */
    public void setCustomer(Customer newCustomer)
    {
    	this.customer = newCustomer;
    }
    
    /**
     * Get the delivery discount of the items on the Order
     * @return deliveryDiscount
     */
    public int getDeliveryDiscount()
    {
        return deliveryDiscount;
    }
    
    /**
     * Set the delivery discount of the items on the Order
     * @param newDeliveryDiscount
     */
    public void setDeliveryDiscount(int newDeliveryDiscount)
    {
    	this.deliveryDiscount = newDeliveryDiscount;
    }

    public Customer getCustomer()
    {
    	return customer;
    }

    /**
     * Calculate the price of the Order
     * @return the totalPrice
     */
    public double calculatePrice()
    {
        int cd = customer.getCraftsmanDiscount();
        int qd = 0;
        int dd = deliveryDiscount;
        
        for(int index = 0; partOrders.size() > index; index++)
        {
            PartOrder partOrder = partOrders.get(index);
            qd += partOrder.calculateQuantityDiscount();
        }
        
        int percent = cd + qd + dd;
        
        boolean done = false;
        while(!done)
        {
        	if(percent <= 20)
            {
            	int subtotal = 0;
            	
            	for(int index = 0; partOrders.size() > index; index++)  // regner forkert 
                {
                    PartOrder partOrder = partOrders.get(index);
                    subtotal += partOrder.getSubTotal();
                }
            	
            	int discount = (subtotal * percent / 100);
            	
            	totalPrice = subtotal - discount;
            	done = true;
            }
            else
            {
            	percent = 20;
            }
        }
        return totalPrice;
    }

    public abstract double calculateTotalPrice();
    
    public ArrayList<PartOrder> getPartOrderList()
    {
    	return partOrders;
    }
}
